package com.bitauto.i.commonUtils.leet;

public class Answer31 {


    public void nextPermutation(int[] nums) {
        int i= nums.length-2;

        while(i>=0 && nums[i]>=nums[i+1]) {
            i--;
        }

        if(i>=0){
            int j=nums.length-1;
            while(j>=0 && nums[j]<=nums[i]){
                j--;
            }
            swap(nums,i,j);
        }
        reversePart(nums,i+1);

    }

    private void swap(int[] nums,int i,int j){
        int t=nums[i];
        nums[i]=nums[j];
        nums[j]=t;
    }

    private void reversePart(int[] nums,int start){
        int i= start;
        int j=nums.length-1;
        while(i<j){
            swap(nums,i,j);
            i++;
            j--;
        }
    }


}
